package com.system.training.web;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.system.common.util.ExtAjaxResponse;
import com.system.common.util.ExtPageable;
import com.system.training.entity.Training;
import com.system.training.entity.dto.TrainingQueryDTO;
import com.system.training.service.ITrainingService;

@Controller
@RequestMapping("/training")
public class TrainingController {
	@Autowired
	private ITrainingService trainingService;
	
	@RequestMapping("/saveOrUpdate")
	public @ResponseBody ExtAjaxResponse saveOrUpdate(Training training) {
		try {
			trainingService.saveOrUpdate(training);
			return new ExtAjaxResponse(true,"操作成功！");
		} catch (Exception e) {
			return new ExtAjaxResponse(false,"操作失败！");
		}
	}
	
	@RequestMapping("/delete")
	public @ResponseBody ExtAjaxResponse delete(Long id) {
		try {
			Training training=trainingService.findOne(id);
			if(null!=training) {
				trainingService.delete(training);
			}
			return new ExtAjaxResponse(true,"操作成功！");
		} catch (Exception e) {
			return new ExtAjaxResponse(false,"操作失败！");
		}
	}
	
	@RequestMapping("/deleteTrainings")
	public @ResponseBody ExtAjaxResponse deleteTrainings(@RequestParam Long[] ids) {
		try {
			List<Long> lists=Arrays.asList(ids);
			if(null!=lists) {
				trainingService.delete(lists);
			}
			return new ExtAjaxResponse(true,"操作成功！");
		} catch (Exception e) {
			return new ExtAjaxResponse(false,"操作失败！");
		}
	}
	
	
	@RequestMapping("/findOne")
	public @ResponseBody Training findOne(@RequestParam Long id) 
	{
		Training training =  trainingService.findOne(id);
		
		return training;
	}
	
	@RequestMapping("/findAll")
	public @ResponseBody List<Training> findAll() 
	{
		 List<Training> lists =  trainingService.findAll();
		
		return lists;
	}
	
	@RequestMapping("/findPage")
	public @ResponseBody Page<Training> findPage(TrainingQueryDTO trainingDTO,ExtPageable pageable) 
	{
		Page<Training> page =  trainingService.findAll(TrainingQueryDTO.getSpecification(trainingDTO), pageable.getPageable());
		
		return page;
	}
}

